#!/usr/bin/env bash
moduleName=$1
modulePort=$2
Tag=$3

echo "传入的变量 ${moduleName}"
echo "传入的变量 ${modulePort}"
moduleDir=/root/pinyou/${moduleName}
[ -d ${moduleDir} ] || mkdir -p  ${moduleDir}

cd ${moduleDir}
#cat>namespace.yaml<<EOF
#apiVersion: v1
#kind: Namespace
#metadata:
#  name: pinyou
#EOF

cat>deployment.yaml<<EOF
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: ${moduleName}
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      project: www
      app: ${moduleName}
  template:
    metadata:
      labels:
        project: www
        app: ${moduleName}
    spec:
      imagePullSecrets:
      - name: harbor-registry-secret
      containers:
      - name: tomcat
        image: 11.0.0.201:1180/pinyou/${moduleName}:${Tag}
        imagePullPolicy: Always
        ports:
        - containerPort: ${modulePort}
          name: web
          protocol: TCP
        #资源限制
        resources:
          requests:
            memory: "256Mi"
            cpu: "200m"
          limits:
            memory: "1024Mi"
            cpu: "500m"
#        livenessProbe:
#          httpGet:
#            path: /
#            port: ${modulePort}
#          initialDelaySeconds: 40
#          timeoutSeconds: 20
#        readinessProbe:
#          httpGet:
#            path: /
#            port: ${modulePort}
#          initialDelaySeconds: 40
#          timeoutSeconds: 20
EOF
cat>service.yaml<<EOF
apiVersion: v1
kind: Service
metadata:
  name: ${moduleName}
  namespace: default
spec:
  selector:
    project: www
    app: ${moduleName}
  ports:
  - name: web
    port: 80
    targetPort: ${modulePort}
  type: NodePort
EOF
hostname=`echo ${moduleName} | tr  -d '-'`
cat>ingress.yaml<<EOF
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ${moduleName}
  namespace: default
spec:
  rules:
    - host: www.${hostname}.kuanghua.com
      http:
        paths:
        - path: /
          backend:
            serviceName: ${moduleName}
            servicePort: 80
EOF


cd ${moduleDir}
/opt/kubernetes/bin/kubectl apply -f namespace.yaml
/opt/kubernetes/bin/kubectl apply -f deployment.yaml
/opt/kubernetes/bin/kubectl apply -f service.yaml
/opt/kubernetes/bin/kubectl apply -f ingress.yaml